/*!
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 2 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program; if not, write to the Free Software
 *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *
 *    Author: flareguner
 *    E-Mail: flareguner@gmail.com
 *    Years: 2010-2011
 */


#include "notificationsconfig.h"
#include "src/platform.h"

#include <QtGui/QLabel>
#include <QtGui/QSpinBox>
#include <QtGui/QCheckBox>
#include <QtGui/QVBoxLayout>
#include <QtGui/QHBoxLayout>
#include <QtGui/QApplication>
#include <QtCore/QEvent>
#include <QtCore/QEvent>
#include <QtCore/QSettings>


// Defaults
const bool
NotificationsConfig::m_default_enable_popup = false,
NotificationsConfig::m_default_enable_tray = false;

const int
NotificationsConfig::m_default_popup_interval = 0,
NotificationsConfig::m_default_tray_interval = 0;


NotificationsConfig::NotificationsConfig(QWidget *parent)
    :QWidget(parent)
{
    createUi();

    connect(popupCheckBox, SIGNAL(toggled(bool)),popupLabel, SLOT(setEnabled(bool)));
    connect(popupCheckBox, SIGNAL(toggled(bool)),popupSpin, SLOT(setEnabled(bool)));

    connect(trayCheckBox, SIGNAL(toggled(bool)),trayLabel,SLOT(setEnabled(bool)));
    connect(trayCheckBox, SIGNAL(toggled(bool)),traySpin,SLOT(setEnabled(bool)));
    changeEvent(new QEvent(QEvent::LanguageChange));

    read();
}

NotificationsConfig::~NotificationsConfig() {
    delete popupLabel;
    delete trayLabel;
    delete popupCheckBox;
    delete trayCheckBox;
    delete popupSpin;
    delete traySpin;
    delete popupLayout;
    delete trayLayout;
    delete mainLayot;//...
}

bool NotificationsConfig::getPopupEnabled() const {
    return popupCheckBox->isChecked();
}

bool NotificationsConfig::getTrayEnabled() const {
    return trayCheckBox->isChecked();
}

int NotificationsConfig::getPopupInterval() const {
    return popupSpin->value();
}

int NotificationsConfig::getTrayInterval() const {
    return traySpin->value();
}

void NotificationsConfig::read() {
    QSettings s(Platform::getOrganizationName(), Platform::getApplicationName());
    s.beginGroup("Notifications");
    popupCheckBox->setChecked(s.value("enablePopup", m_default_enable_popup).toBool());
    trayCheckBox->setChecked(s.value("enableTray", m_default_enable_tray).toBool());

    popupSpin->setValue(s.value("popupInterval", m_default_popup_interval).toInt());
    traySpin->setValue(s.value("trayInterval", m_default_tray_interval).toInt());
}


void NotificationsConfig::truncate() {
    popupCheckBox->setChecked(m_default_enable_popup);
    trayCheckBox->setChecked(m_default_enable_tray);
    popupSpin->setValue(m_default_popup_interval);
    traySpin->setValue(m_default_tray_interval);
}

void NotificationsConfig::changeEvent(QEvent *e) {
    if(e->type() == QEvent::LanguageChange) {
	popupCheckBox->setText(QApplication::tr("Tooltip popup"));
	trayCheckBox->setText(QApplication::tr("Tray Popup"));

	popupLabel->setText(QApplication::tr("Hide after"));
	trayLabel->setText(QApplication::tr("Hide after"));
    }
}

void NotificationsConfig::save() {
    QSettings s(Platform::getOrganizationName(), Platform::getApplicationName());
    s.beginGroup("Notifications");
    s.setValue("enablePopup", popupCheckBox->isChecked());
    s.setValue("enableTray", trayCheckBox->isChecked());
    s.setValue("popupInterval", popupSpin->value());
    s.setValue("trayInterval", traySpin->value());
    s.endGroup();
}


void NotificationsConfig::createUi() {
    popupLabel = new QLabel(this);
    trayLabel = new QLabel(this);

    popupCheckBox = new QCheckBox(this);
    trayCheckBox = new QCheckBox(this);


    popupSpin = new QSpinBox(this);
    traySpin = new QSpinBox(this);

    popupLayout = new QHBoxLayout;
    trayLayout = new QHBoxLayout;

    mainLayot = new QVBoxLayout;

    popupLayout->addWidget(popupLabel);
    popupLayout->addWidget(popupSpin);

    trayLayout->addWidget(trayLabel);
    trayLayout->addWidget(traySpin);

    mainLayot->addWidget(popupCheckBox);
    mainLayot->addLayout(popupLayout);
    mainLayot->addWidget(trayCheckBox);
    mainLayot->addLayout(trayLayout);

    mainLayot->addStretch();

    this->setLayout(mainLayot);
}
